#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glew.h>
#include <glut.h>
#include "datanode.h"
#include "../renderer/renderplugin.h"

using namespace PBVP;

DataNode::DataNode()
{
	m_IsVisible = true;
	m_pRenderer = NULL;
}

void DataNode::setRenderer(RenderPlugin * pRenderer)
{
	if (m_pRenderer)
	{
		delete m_pRenderer;
		m_pRenderer = NULL;
	}
	m_pRenderer = pRenderer;
	m_pRenderer->setDataNode(this);
	emit rendererChanged();
}

void DataNode::drawBoundingbox()
{
	const Vector3 *corners = m_BBox.getAllCorners();
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glDisable(GL_LIGHTING);
	glDisable(GL_CULL_FACE);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_TEXTURE_3D);
	glDisable(GL_TEXTURE_1D);
	glBegin(GL_QUADS);
	//bottom quad
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_BOTTOM].ptr());
	//top quad
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_TOP].ptr());
	//left quad
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_TOP].ptr());
	//right quad
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_BOTTOM].ptr());
	//far quad
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_LEFT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::FAR_RIGHT_BOTTOM].ptr());
	//near quad
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_BOTTOM].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_RIGHT_TOP].ptr());
	glVertex3fv(corners[AxisAlignedBox::NEAR_LEFT_TOP].ptr());
	glEnd();
	glPopAttrib();
}
//////////////////////////////////////////////////////////////////////////
// Public slots
//////////////////////////////////////////////////////////////////////////

void DataNode::updateView()
{
	emit rendererChanged();
}